<template>
  <div>
    <Chart :cdata="cdata" />
  </div>
</template>

<script>
import Chart from './chart.vue'
export default {
  data () {
    return {
      drawTiming: null,
      cdata: {
        year: null,
        weekCategory: [],
        radarData: [],
        radarDataAvg: [],
        maxData: 12000,
        weekMaxData: [],
        weekLineData: []
      }
    }
  },
  components: {
    Chart,
  },
  mounted () {
    this.drawTimingFn();
  },
  beforeDestroy () {
    clearInterval(this.drawTiming);
  },
  methods: {
    drawTimingFn () {
      this.setData();
      this.drawTiming = setInterval(() => {
        this.setData();
      }, 6000);
    },
    setData () {
      // 清空轮询数据
      this.cdata.weekCategory = [];
      this.cdata.weekMaxData = [];
      this.cdata.weekLineData = [];
      this.cdata.radarData = [];
      this.cdata.radarDataAvg = [];

      let dateBase = new Date();
      this.cdata.year = dateBase.getFullYear();
      // 周数据
      for (let i = 0; i < 7; i++) {
        // 日期
        let date = new Date();
        this.cdata.weekCategory.unshift([date.getMonth() + 1, date.getDate()-i].join("/"));

        // 折线图数据
        this.cdata.weekMaxData.push(100);
        let lineValue = Math.round(Math.random() * 10 + 85); // 85-95之间
        this.cdata.weekLineData.push(lineValue);

        // 雷达图数据
        // 实际指标
        let radarDayData = [
          Math.round(Math.random() * 5 + 90),  // 勤务完成率 90-95
          Math.round(Math.random() * 8 + 82),  // 响应时间 82-90
          Math.round(Math.random() * 6 + 84),  // 群众满意度 84-90
          Math.round(Math.random() * 4 + 91),  // 事故处理率 91-95
          Math.round(Math.random() * 7 + 83),  // 巡逻覆盖率 83-90
          Math.round(Math.random() * 5 + 85)   // 设备完好率 85-90
        ];
        this.cdata.radarData.unshift(radarDayData);

        // 目标指标
        let radarDayDataAvg = [
          Math.round(Math.random() * 3 + 92),  // 勤务完成率 92-95
          Math.round(Math.random() * 5 + 85),  // 响应时间 85-90
          Math.round(Math.random() * 4 + 86),  // 群众满意度 86-90
          Math.round(Math.random() * 3 + 92),  // 事故处理率 92-95
          Math.round(Math.random() * 5 + 85),  // 巡逻覆盖率 85-90
          Math.round(Math.random() * 3 + 87)   // 设备完好率 87-90
        ];
        this.cdata.radarDataAvg.unshift(radarDayDataAvg);
      }
    },
    
    // 更新雷达图数据，接收满意度统计API返回的数据
    updateRadarChart(satisfactionData) {
      if (!satisfactionData || !satisfactionData.radarData) return;
      
      clearInterval(this.drawTiming); // 停止随机数据更新
      
      const latestData = satisfactionData.radarData;
      const targetData = satisfactionData.targetData;
      
      if (latestData && targetData) {
        // 准备雷达图数据
        const radarData = [
          parseFloat(latestData.satisfaction_score || 90),
          parseFloat(latestData.response_time || 85),
          parseFloat(latestData.crowd_satisfaction || 85),
          parseFloat(latestData.event_handle_rate || 90),
          parseFloat(latestData.patrol_coverage || 85),
          parseFloat(latestData.equipment_status_rate || 88)
        ];
        
        const radarDataAvg = [
          parseFloat(targetData.satisfaction_score || 95),
          parseFloat(targetData.response_time || 90),
          parseFloat(targetData.crowd_satisfaction || 90),
          parseFloat(targetData.event_handle_rate || 95),
          parseFloat(targetData.patrol_coverage || 90),
          parseFloat(targetData.equipment_status_rate || 95)
        ];
        
        // 更新雷达图数据
        this.cdata.radarData = [[...radarData]];
        this.cdata.radarDataAvg = [[...radarDataAvg]];
      }
    },
    
    // 更新趋势图数据，接收趋势统计API返回的数据
    updateTrendChart(trendData) {
      if (!trendData || !trendData.scoreTrend || !trendData.completionRateTrend) return;
      
      const scoreTrend = trendData.scoreTrend;
      const completionRateTrend = trendData.completionRateTrend;
      
      if (scoreTrend.length > 0 && completionRateTrend.length > 0) {
        // 清空原有数据
        this.cdata.weekCategory = [];
        this.cdata.weekMaxData = [];
        this.cdata.weekLineData = [];
        
        // 处理日期数据
        scoreTrend.forEach(item => {
          this.cdata.weekCategory.push(item.date);
          this.cdata.weekMaxData.push(100); // 最大值固定为100
        });
        
        // 处理完成率趋势数据
        completionRateTrend.forEach(item => {
          this.cdata.weekLineData.push(parseFloat(item.completion_rate || 85).toFixed(1));
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
</style>